var app = angular.module('pveb_12_1', []);

app.config(function($routeProvider) {
    $routeProvider.
      when('/packages', {controller:AllPackagesController, templateUrl:'views/allPackagesView.html'}).
      when('/packages/:packageName', {controller:PackageContentsController, templateUrl:'views/onePackageView.html'}).
	  when('/corpuses', {controller:CorpusesCtrl, templateUrl:'views/corpusesView.html'}).
	  when('/collections', {controller:CollectionsCtrl, templateUrl:'views/collectionView.html'}).
	  when('/collections/:collectionId', {controller:OneCollectionsCtrl, templateUrl:'views/oneCollectionView.html'}).
	  when('/upload', {controller:UploadCtrl, templateUrl:'views/uploadView.html'}).
	  when('/upload/:message', {controller:UploadCtrl, templateUrl:'views/uploadView.html'});});
app.service('mainService', function() {

	this.package_name = "";
	this.packages = null;
	this.contents = null;
	this.previousId = -1;
	this.collections = null;
	this.collection_id = -1;
});

function AllPackagesController($scope, $http, mainService) {

	$http.get('modules/packages/all_packages.php').
		success(function(data, status, headers, config) {
			if(data=="null") {
				mainService.packages = new Array();
			}
			else {
				mainService.packages = data;
			}

			$scope.packages = mainService.packages;
		}).
		error(function(data, status, headers, config){
			$scope.packages = new Array();
	});
}

function PackageContentsController($scope, $http, $routeParams, mainService) {

	mainService.package_name = $routeParams.packageName;

	$scope.packageName = mainService.package_name;

	$http.get('modules/packages/package_contents.php?dir=' + $routeParams.packageName).
		success(function(data, status, headers, config) {
			if(data=="null") {
				mainService.contents = new Array();
			}
			else {
				mainService.contents = data;
			}

			$scope.contents = mainService.contents;
			if (mainService.contents.length > 0) {
				$scope.fileName = mainService.contents[0];

			} else {
				$scope.fileName = "";
				document.getElementById('tagForm').style.visibility = 'hidden';
			}
		}).
		error(function(data, status, headers, config) {
			$scope.contents = new Array();
	});
	
	$http.get('modules/corpus/all_corpuses.php').success(function(data, status, headers, config) {
		$scope.allCorpus = data;
		$scope.selectedCorpus = data[0].id;
	}).error(function(data, status, headers, config) {
		alert(data);
	});

	$scope.contentPath = $routeParams.contentPath;
	var tags = [];
	$scope.importContent = function() {
		var tags = $("#tags").val().split(',');
		var allTags = JSON.stringify(tags);
		//alert($scope.packageName);

		$http.post('modules/content/save_content.php', 'packageName='+ $scope.packageName + "&fileName=" + $scope.fileName + "&tags=" + allTags + "&corpus=" + $scope.selectedCorpus, {headers:{'Content-Type':'application/x-www-form-urlencoded'}}).
			success(function(data, status, headers, config){
				//alert(data);
				var deleteIndex = -1;

				for (var i = 0; i < mainService.contents.length; i++){
					if (mainService.contents[i] == $scope.fileName) {
						deleteIndex = i;
						break;
					}
			    }

			    if (deleteIndex != -1) {
			    	mainService.contents.splice(deleteIndex, 1);
			    	if(mainService.contents.length > 0) {
			    		$scope.fileName = mainService.contents[0];
			    	} else {
			    		$scope.fileName = "";
						document.getElementById('tagForm').style.visibility = 'hidden';
			    	}
			    }
		}).
		error(function(data, status, headers, config){
				$scope.podaci="Greska";
				alert("Greska");
		});
	};

	$scope.selectContent = function(packageName_, content_, index) {
		$scope.contentPath = packageName_;
		$scope.fileName =  content_;

		if(mainService.previousId != -1) {
			document.getElementById("content" + mainService.previousId).style.textDecoration = 'none';
		}

		mainService.previousId = index;
		document.getElementById("content" + index).style.textDecoration = 'underline';
	};
}

function OneCollectionsCtrl($scope, $http, $routeParams, mainService) {

	$http.get('modules/collection/get_collection.php?id=' + $routeParams.collectionId, {headers:{'Content-Type':'application/x-www-form-urlencoded'}})
		.success(function (data, status, headers, config) {
		
		$scope.collectionName1 = data.info.name;
		$scope.collectionDescription1 = data.info.description;
		$scope.coll_tags = data.tags;
		
		//mainService.collections = data;
		//$scope.collections = mainService.collections;
	}).error(function(data,status, headers, config) {
		alert("greska");
	});

	mainService.collection_id = $routeParams.collectionId;
	$scope.collectionId = mainService.collection_id;

	$scope.getCollections = function() {
		$http.get('modules/collection/all_collections.php').success(function (data, status, headers, config) {
			mainService.collections = data;
			$scope.collections = mainService.collections;
		}).error(function(data,status, headers, config) {
			//alert("bla");
		});
	};
	
	document.getElementById("packagesRight").style.visibility = 'hidden';
	document.getElementById("packagesRight").style.width = "0px";
	document.getElementById("oneCollectionRight").style.visibility = 'visible';

	$scope.showForm = function() {
		document.getElementById("packagesRight").style.visibility = 'visible';
		document.getElementById("packagesRight").style.width = "400px";
		document.getElementById("oneCollectionRight").style.visibility = 'hidden';
		$scope.collectionName1 = "";
		$scope.collectionDescription1 = "";
	}

	$scope.getCollections();
	$scope.selected = {};
	$scope.contentDictionary = {};
	
	$scope.searchTags = function() {
		var allTags = $("#tags").val().split(",");
		var tags = JSON.stringify(allTags);
		$http.post('modules/content/get_content.php','tags=' + tags, {headers:{'Content-Type':'application/x-www-form-urlencoded'}})
			.success(function (data, status, headers, config) {
				//alert(data);
			$scope.contents = data;
			for (var i = 0; i < data.length; i++) {
				$scope.selected[data[i].id] = false;
				$scope.contentDictionary[data[i].id] = data[i];
			}
		}).error(function(data,status, headers, config) {
			//alert(data);
		});
	};
	
	$scope.addCollection = function() {
		var tags = JSON.stringify($("#tags").val().split(","));
		
		var allContent = new Array();
		for (var i = 0; i < $scope.contents.length; i++) {
			if ($scope.selected[$scope.contents[i].id] == true) {
				allContent.push($scope.contents[i].id);
			}
		}
		var content = JSON.stringify(allContent);
		
		$http.post('modules/collection/save_collection.php','name=' + $scope.collectionName + '&description=' + $scope.collectionDescription + '&tags=' +
			tags + '&content=' + content, {headers:{'Content-Type':'application/x-www-form-urlencoded'}}).
			success(function (data, status, headers, config) {
				//mainService.collections.push("a"+$scope.collectionName);
				//alert(data);
				$http.get('modules/collection/all_collections.php').success(function (data, status, headers, config) {
					mainService.collections = data;
					$scope.collections = mainService.collections;
					document.getElementById("packagesRight").style.visibility = 'hidden';
					alert("Uspesno ste dodali kolekciju: " + $scope.collectionName);

				}).error(function(data,status, headers, config) {

				});
			}).error(function (data, status, headers, config) {
				//alert("YES");
		});
	}
}

function CollectionsCtrl($scope, $http, mainService) {

	$scope.getCollections = function() {
		$http.get('modules/collection/all_collections.php').success(function (data, status, headers, config) {
			mainService.collections = data;
			$scope.collections = mainService.collections;
		}).error(function(data,status, headers, config) {
			//alert("bla");
		});
	};
	
	document.getElementById("packagesRight").style.visibility = 'hidden';

	$scope.showForm = function() {
		document.getElementById("packagesRight").style.visibility = 'visible';
	}

	$scope.getCollections();
	$scope.selected = {};
	$scope.contentDictionary = {};
	
	$scope.searchTags = function() {
		var allTags = $("#tags").val().split(",");
		var tags = JSON.stringify(allTags);
		$http.post('modules/content/get_content.php','tags=' + tags, {headers:{'Content-Type':'application/x-www-form-urlencoded'}})
			.success(function (data, status, headers, config) {
				//alert(data);
			$scope.contents = data;
			for (var i = 0; i < data.length; i++) {
				$scope.selected[data[i].id] = false;
				$scope.contentDictionary[data[i].id] = data[i];
			}
		}).error(function(data,status, headers, config) {
			alert(data);
		});
	};
	
	$scope.addCollection = function() {
		var tags = JSON.stringify($("#tags").val().split(","));
		
		var allContent = new Array();
		for (var i = 0; i < $scope.contents.length; i++) {
			if ($scope.selected[$scope.contents[i].id] == true) {
				allContent.push($scope.contents[i].id);
			}
		}
		var content = JSON.stringify(allContent);
		
		$http.post('modules/collection/save_collection.php','name=' + $scope.collectionName + '&description=' + $scope.collectionDescription + '&tags=' +
			tags + '&content=' + content, {headers:{'Content-Type':'application/x-www-form-urlencoded'}}).
			success(function (data, status, headers, config) {
				//mainService.collections.push("a"+$scope.collectionName);
				//alert(data);
				$http.get('modules/collection/all_collections.php').success(function (data, status, headers, config) {
					mainService.collections = data;
					$scope.collections = mainService.collections;
					document.getElementById("packagesRight").style.visibility = 'hidden';
					alert("Uspesno ste dodali kolekciju: " + $scope.collectionName);

				}).error(function(data,status, headers, config) {
				});
			}).error(function (data, status, headers, config) {
		});
	}
}

function CorpusesCtrl($scope, $http) {
	
	$scope.getCorpuses = function() {
		
		$http.get('modules/corpus/all_corpuses.php').success(function(data, status, headers, config) {
			$scope.corpuses = data;
		}).error(function(data, status, headers, config) {
		});
	};
	
	$scope.getCorpuses();
	
	$scope.addCorpusName = function() {
		
		$http.post('modules/corpus/save_corpus.php','name=' + $scope.corpusName, {headers:{'Content-Type':'application/x-www-form-urlencoded'}})
		.success(function (data, status, headers, config) {
			$scope.getCorpuses();
		}).error(function(data, status, headers,config) {
		});
	}
}

function UploadCtrl($scope, $routeParams) {
	if ($routeParams.message == "true") {
		$scope.msg = "Uploaded files!";
	}
	else {
		$scope.msg = "";
	}
}